﻿using System.Linq;
using System.Threading.Tasks;
using Leo.Awesome.DictionaryManagement.Domain;
using Leo.Awesome.DictionaryManagement.Events;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;

namespace Leo.Awesome.DictionaryManagement.Handlers
{
    public class LockDictionItemsByCodesHandler : IDistributedEventHandler<LockDictionItemsByCodesEvent>, ITransientDependency
    {
        private readonly IDictionaryItemRepository _dictionaryItemRepository;
        public LockDictionItemsByCodesHandler(IDictionaryItemRepository dictionaryItemRepository)
        {
            _dictionaryItemRepository=dictionaryItemRepository;
        }
        public async Task HandleEventAsync(LockDictionItemsByCodesEvent eventData)
        {
            if (eventData == null || !eventData.Codes.Any())
            {
                return;
            }
            var codes = eventData.Codes;
            var dicitems = await _dictionaryItemRepository.GetListAsync(x => codes.Contains(x.Code));
            foreach (var dicitem in dicitems)
            {
                dicitem.IsLocked = true;
                await _dictionaryItemRepository.UpdateAsync(dicitem);
            }
        }
    }
}
